﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntityObjects;
using DAL_V1.App_Code.DBBroker;
using BAL.Controller;
using System.Data;
using System.Data.SqlClient;

namespace PresentationLayer.View.DepartmentManager
{

    public partial class DelHeadAuthority : System.Web.UI.Page
    {       private static List<Employee> empe_list;
            Employee loginDepHead;
            private static bool flag;
            protected void Page_Load(object sender, EventArgs e)
            {
                
               if (!IsPostBack)
               {
                       loginDepHead = new Employee();
                       loginDepHead = (Employee)Session["CurrentUser"];
                       
                       
                       /******Current Authorized Head*************/
                        Employee curAuthohead = new Employee();
                        if (loginDepHead.EmpId!=null)
                        {
                            
                             curAuthohead = DelHeadAuthorityController.getDelHeadAuthorityController().getEmployeebyDeptIdAndRole(loginDepHead.DeptId.ToString(), 2);
                        }
                        if (curAuthohead != null && curAuthohead.EmpName != null)
                        {
                            lblCurauthoHead.Text = curAuthohead.EmpName.ToString();
                            ViewState["curAuthohead"] = curAuthohead.EmpId;
                        }
                        else
                        {
                            lblCurauthoHead.Text = "NIL";
                            btnRemove.Visible = false;
                        }

                        
                        //Respective Department Employee will be displayed except Department Head.
                        empe_list = new List<Employee>();
                        if (loginDepHead.EmpId != null)
                        empe_list = DelHeadAuthorityController.getDelHeadAuthorityController().getEmployeeByListByManager(loginDepHead.DeptId.ToString(), loginDepHead.EmpId.ToString());
                       
                  }
                        
                     if (flag != true)
                     {
                          Bind(empe_list);
                     }
                      else  flag = false;    
             
            }
            protected void Bind(List<Employee> empe_list)
            {
                if (empe_list.Count == 0)
                {
                    GvEmpList.DataSource = null;
                    GvEmpList.DataBind();
                }
                else
                {
                    GvEmpList.DataSource = empe_list;
                    GvEmpList.DataBind();
                }
            }

            protected void GvEmpList_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                         GvEmpList.PageIndex = e.NewPageIndex;
                         GvEmpList.DataSource = empe_list;
                         GvEmpList.DataBind();

             }

              
            /*Search by Employee name*/
            protected void btnSearch_Click(object sender, EventArgs e)
            {
                        Employee empe = new Employee();
                        empe = (Employee)Session["CurrentUser"];
                        string Depname = empe.DeptId;
                        //string Depname = loginDepHead.DeptId;
                        GvEmpList.DataSource = DelHeadAuthorityController.getDelHeadAuthorityController().getEmployeebyDeptIdAndEmpName(Depname, txtEmpName.Text.Trim());
                        GvEmpList.DataBind();
                        flag = true;

            }

       
            protected void btnselect_Click(object sender, EventArgs e)
            {
                        LinkButton EmpIdButton = sender as LinkButton;
                        int employeeid = Convert.ToInt32(EmpIdButton.CommandArgument);
                        EmpIdButton.Enabled = false;

                        ViewState["selEmp"] = Convert.ToString(employeeid);
                        lblmessage.Visible = true;
                        lblmessage.Text = "Employee ID '" + ViewState["selEmp"] + "' has been selected.";
                 
            }
            /*Assign Department Head Authority to Selected Employee ( Roleid "2" )*/
            protected void btnDelegate_Click(object sender, EventArgs e)
            {
                        string selEmp;
                        string curAuthoheadId;
                        string Toaddress = "";

                /*find selected employeeid row object from empe_List*/
                if (ViewState["selEmp"] == null)
                {
                    lblmessage.Visible = true;
                    lblmessage.Text = "Please select employee to assign.";
                }
                
                else
                {
                     if (empe_list.Count != 0)
                     {
                          selEmp = Convert.ToString(ViewState["selEmp"]);
                          Employee myLocatedObj = empe_list.Find(delegate(Employee ep) { return ep.EmpId == selEmp; });
 
                        /*update Authorized Head Role Id- 2 at Employee table*/
                          if (myLocatedObj != null)
                          {
                              myLocatedObj.RoleId = 2;
                              int result = DelHeadAuthorityController.getDelHeadAuthorityController().updateEmployee(myLocatedObj, selEmp);
                              Send_email(myLocatedObj);
                          }

                         
                         if (ViewState["curAuthohead"] != null)
                         {
                                curAuthoheadId = Convert.ToString(ViewState["curAuthohead"]);
                                Employee myCurHeadobj = empe_list.Find(delegate(Employee ep) { return ep.EmpId == curAuthoheadId; });

                            /*Update current Authorized person to normal Employee Role*/
                             if (myCurHeadobj != null)
                             {
                                myCurHeadobj.RoleId = 5;
                                int rupdate = DelHeadAuthorityController.getDelHeadAuthorityController().updateEmployee(myCurHeadobj, curAuthoheadId);
                                Toaddress = myLocatedObj.Email.ToString();
                                Send_email(myCurHeadobj);
                             }

                          }

                                lblmessage.Text = "Successfully Delegated to Employee ID '" + ViewState["selEmp"] + "'.";
                                Panel.Visible = false;
                              
                      }
                }

         }
          
              
            /*Remove Authority of Current Authorized person  */
            protected void btnRemove_Click(object sender, EventArgs e)
            {
                              string curAuthoheadId;
                              string Toaddress="";
                              if (ViewState["curAuthohead"] == null)
                              {
                                  lblmessage.Visible = true;
                                  lblmessage.Text = "There is no Employee to remove Department Head Authority.";
                              }
                              else
                              {
                                  if (empe_list.Count != 0)
                                  {
                                      curAuthoheadId = Convert.ToString(ViewState["curAuthohead"]);
                                      Employee myLocatedObj = empe_list.Find(delegate(Employee ep) { return ep.EmpId == curAuthoheadId; });
                                      
                                      /*Remove Department Authority from Employee*/
                                      if (myLocatedObj != null)
                                      {
                                          myLocatedObj.RoleId = 5;
                                          int result = DelHeadAuthorityController.getDelHeadAuthorityController().updateEmployee(myLocatedObj, curAuthoheadId);
                                          Toaddress = myLocatedObj.Email.ToString();
                                      }

                                      lblmessage.Visible = true;
                                      lblmessage.Text = "Successfully Remove Authority to Employee ID '" + curAuthoheadId + "'.";
                                      btnRemove.Visible = false;
                                     
                                      //email to person who have removed Authority
                                      //Send_email(Toaddress);
                                      Send_email(myLocatedObj);
                                      
                                  }

                          }
                   
            }


           
            public void Send_email(Employee eObj)
            {
                Email semail = new Email(eObj);
                semail.createMail();

            
            }

            //GV Link button enable/disable
            protected void GvEmpList_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                        if (e.Row.RowType == DataControlRowType.DataRow)
                        {
                             if (e.Row.Cells[1].Text == Convert.ToString(ViewState["selEmp"]))
                              {
                                   ((LinkButton)e.Row.FindControl("btnselect")).Enabled = true;
                              }
                         }

              }

            protected void btnAll_Click(object sender, EventArgs e)
            {
                  Bind(empe_list);
            }

          

          
          
    }
}